package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.GoodImage;
import com.wuming.house.model.GoodsGroup;
import com.wuming.house.service.GoodImageService;
import com.wuming.house.service.GoodsGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author xuhuai
 * @createTime 2019/8/16 17:49
 * @Description:
 */
@Controller
@RequestMapping("/web/goodsGroup")
public class GoodsGroupController extends BaseController {

    @Autowired
    private GoodsGroupService goodsGroupService;
    @Autowired
    private GoodImageService goodImageService;

    @RequestMapping("/toGoodsGroup")
    public String toGoodsGroup() {
        return "/goods/manageGoodsGroup";
    }

    /**
     * @param type  1:保存 2:修改
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toEditGoodsGroup/{type}/{id}")
    public String toEditGoodsGroup(@PathVariable Integer type, @PathVariable Integer id, Model model) {
        if (type == 2) {
            if (null != id) {
                model.addAttribute("goodsGroup", goodsGroupService.getGoodsGroupById(id));
            }
        }
        return "/goods/editGoodsGroup";
    }

    @ResponseBody
    @RequestMapping("list")
    public CommonResult list(@RequestParam(required = false) String name,
                             @RequestParam(defaultValue = "1") int pageIndex,
                             @RequestParam(defaultValue = "10") int pageSize) {
        logger.info("准备查询商品分组");
        try {
            List<GoodsGroup> goodsGroups = goodsGroupService.getGoodsGroupByPage(name, pageIndex, pageSize);
            int total = goodsGroupService.getTotal(name);
            return CommonResult.createSuccessResult(goodsGroups, total, "查询商品分组成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.createFailureResult("查询商品分组失败");
        }
    }

    @ResponseBody
    @RequestMapping("edit")
    public JsonResult edit(GoodsGroup goodsGroup) {
        if (null == goodsGroup.getId()) {
            goodsGroup.setCreateDatetime(new Date());
            goodsGroupService.saveGoodsGroup(goodsGroup);
            return new JsonResult(null, "添加分组成功");
        } else {
            goodsGroupService.modifyGoodsGroup(goodsGroup);
            return new JsonResult(null, "更新分组成功");
        }
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    public JsonResult delete(@PathVariable Integer id) {
        try {
            goodsGroupService.deleteGoodsGroupById(id);
            return new JsonResult(null, "删除商品分组成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(null, "删除商品分组失败", false);
        }
    }

    @ResponseBody
    @RequestMapping("/getGoodImage")
    public JsonResult getGoodImage(@RequestParam(value = "id") Integer imageId) {
        try {
            String path = this.getImagePath(imageId, goodImageService);
            return new JsonResult(path, "", true);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new JsonResult(null, "", false);
        }
    }

    @ResponseBody
    @RequestMapping("/uploadImg")
    public JsonResult uploadImg(MultipartFile file, HttpServletRequest request) {
        try {
            String path = this.uploadImage(file, "goodsGroup", 1);
            logger.info("文件成功上传到指定目录下,文件目录:{},", path);
            GoodImage goodImage = new GoodImage();
            goodImage.setImagePath(path);
            goodImage.setCreateDatetime(new Date());
            goodImageService.saveImage(goodImage);
            return new JsonResult(goodImage.getId(), "上传成功", true);
        } catch (IOException ie) {
            ie.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult("", "上传失败", false);
    }
}
